package com.operate.service;

import com.operate.bean.VO.AvgWaitTimeVO;
import com.operate.bean.VO.DinersNumStatVO;
import com.operate.bean.VO.ShareNumVO;
import com.operate.bean.VO.WRMonthNumVO;
import com.operate.dao.WaitMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ZeckZheng
 * @time: 2023/11/6 3:26
 */
@Slf4j
@Service
public class WaitService {
    @Autowired
    private WaitMapper waitMapper;

    /**
     * @name: waitMonthList
     * @description: 等位 渠道数量统计（近12个月）
     * @auther: ZeckZheng
     */
    public List<WRMonthNumVO> waitMonthList() {
        List<WRMonthNumVO> list = new ArrayList<>();
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM");
        for (int i = 11; i >= 0; i--) {
            String statTime = localDate.minusMonths(i).format(dtf);
            WRMonthNumVO vo = new WRMonthNumVO();
            vo.setType("W");
            vo.setStatTime(statTime);
            vo.setH5Num(0L);
            vo.setIpadNum(0L);
            vo.setKioskNum(0L);
            list.add(vo);
        }
        List<WRMonthNumVO> waitList = waitMapper.waitMonthList();
        Map<String, WRMonthNumVO> map = new HashMap<>();
        if (waitList != null && !waitList.isEmpty()) {
            for (WRMonthNumVO vo : waitList) {
                map.put(vo.getStatTime(), vo);
            }
        }
        for (WRMonthNumVO vo : list) {
            if (map.containsKey(vo.getStatTime())) {
                vo.setH5Num(map.get(vo.getStatTime()).getH5Num());
                vo.setKioskNum(map.get(vo.getStatTime()).getKioskNum());
                vo.setIpadNum(map.get(vo.getStatTime()).getIpadNum());
            }
        }
        return list;

    }

    /**
     * @name: dinersNumStatList
     * @description: 就餐人数规模统计
     * @auther: ZeckZheng
     */
    public List<DinersNumStatVO> dinersNumStatList() {
        List<DinersNumStatVO> list = new ArrayList<>();
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM");
        for (int i = 11; i >= 0; i--) {
            String statTime = localDate.minusMonths(i).format(dtf);
            DinersNumStatVO vo = new DinersNumStatVO();
            vo.setType("W");
            vo.setStatTime(statTime);
            vo.setLevel1(0L);
            vo.setLevel2(0L);
            vo.setLevel3(0L);
            list.add(vo);
        }

        List<DinersNumStatVO> dinersList = waitMapper.dinersNumStatList();
        Map<String, DinersNumStatVO> map = new HashMap<>();
        if (dinersList != null && !dinersList.isEmpty()) {
            for (DinersNumStatVO vo : dinersList) {
                map.put(vo.getStatTime(), vo);
            }
        }
        for (DinersNumStatVO vo : list) {
            if (map.containsKey(vo.getStatTime())) {
                vo.setLevel1(map.get(vo.getStatTime()).getLevel1());
                vo.setLevel2(map.get(vo.getStatTime()).getLevel2());
                vo.setLevel3(map.get(vo.getStatTime()).getLevel3());
            }
        }
        return list;
    }

    public List<ShareNumVO> waitRegularCustomerStat() {
        List<ShareNumVO> list = new ArrayList<>();
//        LocalDate localDate = LocalDate.now();
//        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM");
//        for (int i = 11; i >= 0; i--) {
//            String statTime = localDate.minusMonths(i).format(dtf);
//            ShareNumVO vo = new ShareNumVO();
//            vo.setStatTime(statTime);
//            vo.setNum1(0L);
//            vo.setNum2(0L);
//            vo.setMark("W");
//            list.add(vo);
//        }
//        Map<String, Map<String, Object>> regularMap = waitMapper.waitRegularCustomer();
//        Map<String, Map<String, Object>> totalMap = waitMapper.waitTotalCustomerMonth();
//        for (ShareNumVO vo : list) {
//            if (regularMap.containsKey(vo.getStatTime())) {
//                vo.setNum1((Long) regularMap.get(vo.getStatTime()).get("regularCustomer"));
//            }
//            if (totalMap.containsKey(vo.getStatTime())) {
//                vo.setNum2((Long) totalMap.get(vo.getStatTime()).get("totalCustomer"));
//            }
//        }
        return list;
    }

    /**
     * @name: todayAvgWaitTimeStat
     * @description: 平均等位时间
     * @auther: ZeckZheng
     */
    public List<ShareNumVO> todayAvgWaitTimeStat() {
        List<ShareNumVO> list = new ArrayList<>();
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (int i = 0; i <= 23; i++) {
            ShareNumVO vo = new ShareNumVO();
            vo.setName("平均等待时间");
            if (i < 10) {
                vo.setStatTime("0" + i);
            } else {
                vo.setStatTime("" + i);
            }
            vo.setAvgWaitTime(0L);
            list.add(vo);
        }

        List<AvgWaitTimeVO> waitList = waitMapper.todayAvgWaitTimeStat();
        Map<String, AvgWaitTimeVO> map = new HashMap<>();
        if (waitList != null && !waitList.isEmpty()) {
            for (AvgWaitTimeVO vo : waitList) {
                map.put(vo.getStatTime(), vo);
            }
        }
        for (ShareNumVO shareNumVO : list) {
            if (map.containsKey(shareNumVO.getStatTime())) {
                shareNumVO.setAvgWaitTime(map.get(shareNumVO.getStatTime()).getAvgMinute());
            }

        }
        return list;
    }

    /**
     * @name: waiTimeSlotNumStatByYear
     * @description: 近12个月等位数量时间段统计
     * @auther: ZeckZheng
     */
    public List<ShareNumVO> waitNumTimeSlotStatByMonth() {
        List<ShareNumVO> list = new ArrayList<>();
        for (int i = 0; i <= 23; i++) {
            ShareNumVO vo = new ShareNumVO();
            vo.setName("时间段等位数量");
            if (i < 10) {
                vo.setStatTime("0" + i);
            } else {
                vo.setStatTime("" + i);
            }
            vo.setWaitNum(0L);
            list.add(vo);
        }
        Map<String, Map<String, Object>> map = waitMapper.waitNumTimeSlotStatByMonth();
        for (ShareNumVO shareNumVO : list) {
            if (map.containsKey(shareNumVO.getStatTime())) {
                shareNumVO.setWaitNum((Long) map.get(shareNumVO.getStatTime()).get("waitNum"));
            }
        }
        return list;
    }

}
